/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.provider;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;

import java.util.ArrayList;
import java.util.List;

/**
 * 可伸缩布局adapter
 *
 * @since 2021-04-08
 */
public class ExpandableLayoutAdapter extends BaseItemProvider {
    private List<String> beans = new ArrayList<>();

    /**
     * 构造方法
     */
    public ExpandableLayoutAdapter() {
        beans.add("可伸缩布局简单使用");
        beans.add("水平伸缩使用");
        beans.add("在RecycleView中使用");
    }

    @Override
    public int getCount() {
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder holder;
        if (component == null) {
            Context context = componentContainer.getContext();
            holder = new ViewHolder();
            cpt = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_item_expandable_layout, null, false);
            holder.title = (Text) cpt.findComponentById(ResourceTable.Id_title);
            cpt.setTag(holder);
        } else {
            cpt = component;
            holder = (ViewHolder) cpt.getTag();
        }

        holder.title.setText(beans.get(position));
        return cpt;
    }

    /**
     * holder
     *
     * @since 2021-04-08
     */
    static class ViewHolder {
        public Text title;
    }
}
